package net.cmkj.mine.util.tpp.coder;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.generator.MappingKitGenerator;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;

public class WMappingKitGenerator extends MappingKitGenerator {

	public WMappingKitGenerator(String mappingKitPackageName, String mappingKitOutputDir) {
		super(mappingKitPackageName, mappingKitOutputDir);
	}

	@Override
	public void generate(List<TableMeta> tableMetas) {
		System.out.println("Generate MappingKit file ...");
		System.out.println("MappingKit Output Dir: " + mappingKitOutputDir);
		
		Engine engine = Engine.use("forMappingKit");
		if (engine == null) {
			engine = Engine.create("forMappingKit");
			engine.setSourceFactory(new ClassPathSourceFactory());
			engine.addSharedMethod(new StrKit());
		}
		
		Kv data = Kv.by("mappingKitPackageName", mappingKitPackageName);
		//data.set("mappingKitClassName", mappingKitClassName);
		data.set("mappingKitClassName", "MappingKit");
		data.set("tableMetas", tableMetas);
		
		String ret = engine.getTemplate(template).renderToString(data);
		
		readOldMapping(ret, tableMetas.get(0).name);
	}
	
	/**
	 * 读取之前mapping生成的数据，然后进行追加
	 * @param add
	 * @param addTableName
	 */
	public void readOldMapping(String add, String addTableName) {
		//String path = mappingKitOutputDir + File.separator + mappingKitClassName + ".java";
		String path = mappingKitOutputDir + File.separator + "MappingKit" + ".java";
		File file = new File(path);
		BufferedReader reader = null;
		if (file.exists()) {
			try {
				reader = new BufferedReader(new FileReader(file));
				String tempString = null;
				boolean start = false;
				boolean exist = false;
				// 一次读入一行，直到读入null为文件结束
				while ((tempString = reader.readLine()) != null) {
					if (start && tempString.equals("	}")) {
						start = false;
					}
					if (start && !tempString.trim().equals("")) {
						int tempi = tempString.indexOf("/");
						if (tempi > -1) {
							String tableName = tempString.substring(tempi + 1, tempString.indexOf("/", tempi + 1));
							if (!tableName.equals("") && tableName.equals(addTableName)) {
								System.out.println(tableName + "is exist!!!");
								exist = true;
							}
						}
					}
					if (tempString.equals("	public static void mapping(ActiveRecordPlugin arp) {")) {
						start = true;
					}
				}
				if (!exist) {
					this.append(path, add);
				}
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (reader != null) {
					try {
						reader.close();
					} catch (IOException e1) {
					}
				}
			}
		}
	}
	
	public void append(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));// true,进行追加写。
			out.write(conent);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
